package ar.com.android.restoapp.providers;

import java.util.ArrayList;
import java.util.List;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;
import ar.com.android.restoapp.dbHelpers.SQLFoodOrderHelper;
import ar.com.android.restoapp.entities.Tables;

public class TablesProvider {
	private static final String TAG = "TablesProvider";
	
	private static final String DB_COLUMN_0_NAME = "idTables";
	private static final String DB_COLUMN_1_NAME = "description";
	private static final String DB_COLUMN_2_NAME = "state";
	private static final String DB_COLUMN_3_NAME = "quantityGuest";
	private static final String DB_COLUMN_4_NAME = "comments";

	private SQLiteDatabase database;
	private SQLFoodOrderHelper dbHelper;
	private String[] allColumns = {DB_COLUMN_0_NAME, DB_COLUMN_1_NAME, DB_COLUMN_2_NAME, DB_COLUMN_3_NAME, DB_COLUMN_4_NAME};
	
	public TablesProvider(Context context) {
		dbHelper = SQLFoodOrderHelper.getHelper(context);
		database = dbHelper.getWritableDatabase();
	}
		
	public long insertTable(Tables table)
	{
		ContentValues contentValues = new ContentValues();
		contentValues.put(DB_COLUMN_0_NAME, table.getIdTable());
		contentValues.put(DB_COLUMN_1_NAME, table.getDescription());
		contentValues.put(DB_COLUMN_2_NAME, table.getState());
		contentValues.put(DB_COLUMN_3_NAME, table.getQuantityGuest());
		contentValues.put(DB_COLUMN_4_NAME, table.getComments());
		
		long insertId = database.insert(SQLFoodOrderHelper.DB_TABLES_TABLE_NAME, null, contentValues);
		return insertId;
	}
	
	public boolean removeTables(Integer idTable)
	{
		int result = database.delete(SQLFoodOrderHelper.DB_TABLES_TABLE_NAME, DB_COLUMN_0_NAME + " = " + idTable, null);
				
		if(result > 0)            
			return true;        
		else            
			return false;
	}
	
	public boolean removeTables()
	{
		int result = database.delete(SQLFoodOrderHelper.DB_TABLES_TABLE_NAME, null, null);
				
		if(result > 0)            
			return true;        
		else            
			return false;
	}
		
	public long updateTable(Tables table)
	{
		ContentValues contentValues = new ContentValues();
		contentValues.put(DB_COLUMN_1_NAME, table.getDescription());
		contentValues.put(DB_COLUMN_2_NAME, table.getState());
		contentValues.put(DB_COLUMN_3_NAME, table.getQuantityGuest());
		contentValues.put(DB_COLUMN_4_NAME, table.getComments());
		
		return database.update(SQLFoodOrderHelper.DB_TABLES_TABLE_NAME, contentValues, DB_COLUMN_0_NAME + " = " + table.getIdTable(), null);
	}
	
	public Tables getTable(Integer idTable)
	{
		Cursor cursor = null;
		Tables table = null;
		
		try
		{
			cursor = database.query(SQLFoodOrderHelper.DB_TABLES_TABLE_NAME, allColumns, DB_COLUMN_0_NAME + " = " + idTable, null, null, null, null);
			cursor.moveToFirst();
			
			table = new Tables();
			table.setIdTable(cursor.getInt(0));
			table.setDescription(cursor.getString(1));
			table.setState(cursor.getString(2));
			table.setQuantityGuest(cursor.getInt(3));
			table.setComments(cursor.getString(4));

			cursor.close();
			return table;
	
		}
		catch (Exception e) {
			Log.e(TAG, e.getMessage());
			return table;
		}
		finally {
			cursor.close();
		}		
	}
	
	public List<Tables> getAllTables() 
	{
		Cursor cursor = null;
		List<Tables> tables = new ArrayList<Tables>();
		
		try
		{
			cursor = database.query(SQLFoodOrderHelper.DB_TABLES_TABLE_NAME, allColumns, null, null, null, null, null);
			cursor.moveToFirst();
			
			while (!cursor.isAfterLast()) {
				Tables table = new Tables();
				table.setIdTable(cursor.getInt(0));
				table.setDescription(cursor.getString(1));
				table.setState(cursor.getString(2));
				table.setQuantityGuest(cursor.getInt(3));
				table.setComments(cursor.getString(4));
				
				tables.add(table);
				cursor.moveToNext();
			}
			return tables;
		}
		catch (Exception e) {
			Log.e(TAG, e.getMessage());
			return tables;
		}
		finally {
			cursor.close();
		}
	}
}
